package com.gopay.webtest.controller.entrustpay;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.util.BaseSign;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.util.ReflectUtil;
import com.gopay.webtest.vo.PayInfo;
import com.gopay.webtest.vo.entrustpay.AuthResultQuery;
import com.gopay.webtest.xml.XmlDao;
import org.apache.commons.lang.time.StopWatch;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * Created by PanShiChao on 2016/12/26.
 */
@Controller
@RequestMapping("/entrustPay")
public class DSAuthResultController extends BaseController {
    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "paySignHandler")
    private BaseSign paySignHandler;

    /**
     * 跳转到申请页面
     *
     * @return
     */
    @RequestMapping("/show")
    public ModelAndView apply() {
        ModelAndView mv = new ModelAndView();

        try {
            //
            Map<String, String> map = xmlDao.readData4Map("ds_auth_result_query", "apply");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("transTime")) {
                map.remove("transTime");
                map.put("transTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("ds.auth.result.query");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到鉴权页面", e);
        }

        return mv;
    }

    /**
     * 执行申请
     *
     * @return
     */
    @RequestMapping("/dsAuthResultQuery")
    public ModelAndView doApply(AuthResultQuery authResultQuery) throws Exception{
        ModelAndView mav = new ModelAndView();

        StopWatch sw  = new StopWatch();

        // 设置MD5加密字段
        try {
            sw.start();
            paySignHandler.sign(authResultQuery.getTranCode() + "_" + authResultQuery.getVersion(), "signType", "signValue", authResultQuery, getRequest());
            sw.stop();
            logger.info("step1 time:"+sw.getTime());
            logger.info("后台notify密文值:" + authResultQuery.getSignValue());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        try {
            ReflectUtil.desField(authResultQuery, authResultQuery.getVerficationCode());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!authResultQuery.getCharset().equals("2")) {
            mav.setViewName("ds.auth.result.query.gbk");
        } else {
            mav.setViewName("ds.auth.result.query.utf8");
        }
        mav.addObject("authResultQuery", authResultQuery);
        return mav;
    }
}
